package com.office.sysManage.roleManage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.office.sysManage.deptManage.pojo.Dept;
import com.office.sysManage.roleManage.dao.RoleMapper;
import com.office.sysManage.roleManage.pojo.Role;
import com.office.sysManage.roleManage.pojo.RoleAndLimits;
import com.office.sysManage.roleManage.service.RoleService;
import com.office.sysManage.staffManage.dao.StaffMapper;
import com.office.sysManage.staffManage.pojo.Staff;
import com.office.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 88168
 * @since 2022-10-24
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private StaffMapper staffMapper;

    public RoleMapper getRoleMapper() {
        return roleMapper;
    }
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    public StaffMapper getStaffMapper() {
        return staffMapper;
    }
    public void setStaffMapper(StaffMapper staffMapper) {
        this.staffMapper = staffMapper;
    }


    /**
     * 根据查询条件查询角色信息
     * @param role 角色和权限对象
     * @return 角色集合
     */
    @Override
    public List<Role> queryRolesByCondition(RoleAndLimits role) {
        ArrayList<Role> roles = new ArrayList<>();
        List<RoleAndLimits> roleAndLimits = roleMapper.queryRolesByCondition(role);
        for (RoleAndLimits r : roleAndLimits) {
            if (inList(roles,r)){
                for (Role oneRole : roles) {
                    if (oneRole.getRoleName().equals(r.getRoleName())){
                        oneRole.getModules().add(r.getModule());
                    }
                }
            }else {
                Role role1 = new Role();
                role1.setId(r.getId());
                role1.setRoleName(r.getRoleName());
                ArrayList<String> modules = new ArrayList<>();
                modules.add(r.getModule());
                role1.setModules(modules);
                roles.add(role1);
            }
        }
        return roles;
    }

    /**
     * 判断列表中是否有同样的角色
     * @param roles 角色列表
     * @param role 角色权限对象
     * @return
     */
    private boolean inList(ArrayList<Role> roles,RoleAndLimits role){
        for (Role r : roles) {
            if (r.getRoleName().equals(role.getRoleName())){
                return true;
            }
        }
        return false;
    }

    /**
     * 添加角色
     * @param role 角色和权限对象
     * @return 是否添加成功
     */
    @Override
    public boolean addRole(Role role) {
        boolean flag = true;
        RoleAndLimits roleAndLimits = new RoleAndLimits();
        roleAndLimits.setRoleName(role.getRoleName());
        // 添加角色
        int i1 = roleMapper.addRole(roleAndLimits);
        if (i1 > 0){
            for (String s : role.getModules()) {
                RoleAndLimits r = new RoleAndLimits();
                // 设置模块ID
                int moduleId = roleMapper.queryModuleIdByName(s);
                r.setModuleId(moduleId);
                // 设置角色ID
                int roleId = roleMapper.queryRoleIdByName(role.getRoleName());
                r.setId(roleId);
                // 添加角色权限
                int i = roleMapper.addRoleLimit(r);
                if (i == 0){
                    flag = false;
                }
            }
        }else {
            return false;
        }
        return flag;
    }

    /**
     * 添加角色权限
     * @param role 角色和权限对象
     * @return 是否添加成功
     */
    @Override
    public boolean addRoleLimit(RoleAndLimits role) {
        return roleMapper.addRoleLimit(role) >= 1;
    }

    /**
     * 修改角色信息
     * @param role 角色和权限对象
     * @return 是否成功
     */
    @Override
    public boolean updateRoleAndLimit(Role role) {
        // 获取当前角色ID
        int roleId = roleMapper.queryRoleIdByName(role.getRoleName());
        role.setId(roleId);
        // 修改角色名
        RoleAndLimits r = new RoleAndLimits();
        r.setId(roleId);
        r.setRoleName(role.getRoleName());
        boolean b = roleMapper.updateRole(r);
        if (b){
            boolean flag = true;
            // 清除当前角色原有的所有权限
            RoleAndLimits clearRole = new RoleAndLimits();
            clearRole.setId(roleId);
            if (roleMapper.updateLimitOne(clearRole) == 0){
                flag = false;
            }
            // 循环添加修改之后的权限
            for (String s : role.getModules()) {
                RoleAndLimits roleAndLimit = new RoleAndLimits();
                roleAndLimit.setModule(s);
                roleAndLimit.setId(roleId);
                if (!updateLimit(roleAndLimit)){
                    flag = false;
                }
            }
            // 修改角色权限
            return flag;
        }
        return false;
    }

    /**
     * 修改角色权限
     * @param role 角色和权限对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateLimit(RoleAndLimits role) {
        // 设置模块ID
        int moduleId = roleMapper.queryModuleIdByName(role.getModule());
        role.setModuleId(moduleId);
        return roleMapper.updateLimitTwo(role) > 0;
    }

    /**
     *
     * @param role 角色对象
     * @return 是否删除成功
     */
    @Override
    @Transactional
    public boolean deleteRole(Role role) {// 设置要删除的角色ID
        Integer roleId = roleMapper.queryRoleIdByName(role.getRoleName());
        role.setId(roleId);
        Staff staff = new Staff();
        staff.setRoleId(roleId);
        if (staffMapper.queryStaffByCondition(staff).size()==0) {
            if (roleMapper.deleteRole(role) >= 1) {
                return roleMapper.deleteLimit(role) >= 1;
            }
        }
        return false;
    }

    /**
     * 通过角色名查询ID
     * @param roleName 角色名称
     * @return 角色ID
     */
    @Override
    public int queryRoleIdByName(String roleName) {
        if (roleName != null && roleName != ""){
            return roleMapper.queryRoleIdByName(roleName);
        }
        return 0;
    }

    /**
     * 根据角色名称查询角色信息
     * @param role 角色对象
     * @return 角色对象
     */
    @Override
    public Role queryRoleInfo(Role role) {
        RoleAndLimits roleAndLimits = new RoleAndLimits();
        roleAndLimits.setRoleName(role.getRoleName());
        // 将RoleAndLimits对象转成Role对象
        ArrayList<String> modules = new ArrayList<>();
        for (RoleAndLimits r : roleMapper.queryRolesByCondition(roleAndLimits)) {
            modules.add(r.getModule());
        }
        Role role1 = new Role();
        role1.setRoleName(role.getRoleName());
        role1.setModules(modules);
        return role1;
    }

}
